// src/main/java/com/neusoft/SPNursingHome/controller/BedRecordController.java
package com.neusoft.SPNursingHome.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.SPNursingHome.po.BedRecord;
import com.neusoft.SPNursingHome.po.BedRecordWithClient;
import com.neusoft.SPNursingHome.po.ResponseBean;
import com.neusoft.SPNursingHome.service.BedRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/bedRecord")
public class BedRecordController {

    @Autowired
    private BedRecordService service;

    /** 查询所有（不分页） */
    @GetMapping("/list")
    public ResponseBean<List<BedRecord>> listAll() {
        return service.listAll();
    }

    /** 分页查询 */
    @GetMapping("/page")
    public ResponseBean<IPage<BedRecord>> listPage(
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "10") long size) {
        return service.listPage(page, size);
    }

    /*分页多条件查询接口*/
    @GetMapping("/search")
    public ResponseBean<IPage<BedRecordWithClient>> search(
            @RequestParam(required = false) String clientName,
            @RequestParam(required = false) String beginTimeStr,
            @RequestParam(required = false) String endTimeStr,
            @RequestParam(required = false) Integer bedRecordStatus,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "10") long size
    ) {
        Date beginTime = null, endTime = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (beginTimeStr != null && !beginTimeStr.isBlank()) {
                beginTime = fmt.parse(beginTimeStr);
            }
            if (endTimeStr != null && !endTimeStr.isBlank()) {
                endTime = fmt.parse(endTimeStr);
            }
        } catch (ParseException e) {
            return new ResponseBean<>(400, "日期格式应为 yyyy-MM-dd");
        }
        return service.listWithClientByCondition(
                clientName, beginTime, endTime, bedRecordStatus, page, size
        );
    }

    /** 根据 ID 查询 */
    @GetMapping("/{id}")
    public ResponseBean<BedRecord> getById(@PathVariable Integer id) {
        return service.getById(id);
    }

    /** 新增 */
    @PostMapping("/create")
    public ResponseBean<Void> create(@RequestBody BedRecord record) {
        return service.create(record);
    }

    /** 更新 */
    @PutMapping("/update")
    public ResponseBean<Void> update(@RequestBody BedRecord record) {
        return service.update(record);
    }

    /** 删除 */
    @DeleteMapping("/delete/{id}")
    public ResponseBean<Void> delete(@PathVariable Integer id) {
        return service.deleteById(id);
    }

    @PostMapping("/swap")
    public ResponseBean<Void> swapBed(
            @RequestParam Integer oldBedId,
            @RequestParam Integer newBedId,
            @RequestParam String newBeginTime,  // 格式 "yyyy-MM-dd HH:mm:ss"
            @RequestParam(required = false) String newEndTime
    ) {
        Date beginTime = null, endTime = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (newBeginTime != null && !newBeginTime.isBlank()) {
                beginTime = fmt.parse(newBeginTime);
            }
            if (newEndTime != null && !newEndTime.isBlank()) {
                endTime = fmt.parse(newEndTime);
            }
        } catch (ParseException e) {
            return new ResponseBean<>(400, "日期格式应为 yyyy-MM-dd HH:mm:ss");
        }
        return service.swapBed(oldBedId,newBedId,beginTime,endTime);
    }
}
